home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / program / mc.lha / MC / mc_spots.c < prev    next >
C/C++ Source or Header  |  1995-11-06  |  7KB  |  352 lines

  1. /******************************************************************************
  2. **                                         **
  3. ** MultiColor-Demo-Spots                             **
  4. **                                         **
  5. **---------------------------------------------------------------------------**
  6. ** V2.0 vom 02.10.95                                 **
  7. ******************************************************************************/
  8.  
  9. #include "sc:source/mc/multicolor.h"
  10.  
  11. /* Protos */
  12.  
  13. void OpenAll(void);
  14. void CloseAll(void);
  15. void Spots(void);
  16. void ReadSpots(char *name);
  17. void DrawSpot(UBYTE i);
  18. double Intensity(double dist);
  19. UBYTE GetSign(UWORD x,UWORD y,UBYTE typ);
  20. void SetSign(UWORD x,UWORD y,UBYTE typ,double color);
  21. void Usage(void);
  22.  
  23. /* Global */
  24.  
  25. struct
  26. {
  27.     UWORD   x,y;
  28.     MCPoint color;
  29.     UWORD   radiusx,radiusy;
  30. } SpotList[50];
  31.  
  32. UBYTE spotanz;
  33.  
  34. int IntensTable[100];
  35.  
  36. /* defines */
  37.  
  38. extern struct ExecBase        *SysBase;
  39. struct IntuitionBase        *IntuitionBase=0l;
  40. struct GfxBase            *GfxBase=0l;
  41. struct Screen            *scr=0l;
  42. struct Window            *win=0l;
  43. MCHandle            *mch=0l;
  44.  
  45. struct TagItem scrtags[]={
  46.     SA_Left,        0,
  47.     SA_Top,         0,
  48.     SA_Width,        0,
  49.     SA_Height,        0,
  50.     SA_Depth,        0,
  51.     SA_Colors,        0l,
  52.     SA_Type,        CUSTOMSCREEN,
  53.     SA_DisplayID,        PAL_MONITOR_ID,
  54.     TAG_DONE
  55. };
  56.  
  57. struct TagItem wintags[]={
  58.     WA_Left,        0,
  59.     WA_Top,         0,
  60.     WA_Width,        0,
  61.     WA_Height,        0,
  62.     WA_IDCMP,        IDCMP_MOUSEBUTTONS|IDCMP_RAWKEY,
  63.     WA_Flags,        WFLG_SMART_REFRESH|WFLG_RMBTRAP|
  64.                 WFLG_BORDERLESS|WFLG_ACTIVATE,
  65.     WA_CustomScreen,0l,
  66.     TAG_DONE
  67. };
  68.  
  69. /* Funktions */
  70.  
  71. void OpenAll(void)
  72. {
  73.     if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",39))) CloseAll();
  74.     if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37))) CloseAll();
  75.  
  76.     if(!(scr=OpenScreenTagList(0l,scrtags))) CloseAll();
  77.     wintags[6].ti_Data=(ULONG)scr;
  78.  
  79.     if(!(win=OpenWindowTagList(0l,wintags))) CloseAll();
  80. }
  81.  
  82. void CloseAll(void)
  83. {
  84.     if(win)                 CloseWindow(win);
  85.     if(scr)                 CloseScreen(scr);
  86.     if(GfxBase)             CloseLibrary((struct Library *)GfxBase);
  87.     if(IntuitionBase)       CloseLibrary((struct Library *)IntuitionBase);
  88.     exit(0);
  89. }
  90.  
  91. LONG IntSqrt (LONG num)
  92. {
  93.     LONG left, right, mid;
  94.  
  95.     left = 0; right = num;
  96.  
  97.     while (right-left > 1)
  98.     {
  99.     mid=(left+right+1)>>1;
  100.  
  101.     if (mid*mid > num)
  102.         right = mid;
  103.     else
  104.         left = mid;
  105.     }
  106.  
  107.     if (left!=right)
  108.     {
  109.     int tmp;
  110.  
  111.     mid=left*left;
  112.     tmp=right*right;
  113.  
  114.     if (num-mid < tmp-num)
  115.         mid=left;
  116.     else
  117.         mid=right;
  118.     }
  119.     else
  120.     mid=left;
  121.  
  122.     return mid;
  123. }
  124.  
  125. void Spots(void)
  126. {
  127.     struct IntuiMessage *imsg;
  128.     ULONG iclass;
  129.     USHORT icode;
  130.     register UBYTE i=0,quit=0;
  131.     register UWORD x,y;
  132.     register LONG xp,yp;
  133.     int intens,idist;
  134.     MCPoint akt;
  135.  
  136.     for (i=0; i<=90; i++)
  137.     {
  138.     IntensTable[i] = 256.0 * cos((PID2/90.0)*(double)i);
  139.     }
  140.  
  141.     for ( ; i<=100; i++)
  142.     {
  143.     IntensTable[i] = 0;
  144.     }
  145.  
  146.     for(y=0;y<mch->yres&&!quit;y++)
  147.     {
  148.     for(x=0;x<mch->xres&&!quit;x++)
  149.     {
  150.         akt.r=akt.g=akt.b=0;
  151.         for(i=0;i<spotanz;i++)
  152.         {
  153.         yp=y-SpotList[i].y;
  154.         xp=x-SpotList[i].x;
  155.  
  156.         if (xp < 0)
  157.             xp = -xp;
  158.         if (yp < 0)
  159.             yp = -yp;
  160.  
  161.         if (xp > SpotList[i].radiusx || yp > SpotList[i].radiusx)
  162.             continue;
  163.  
  164.         idist=IntSqrt (xp*xp+yp*yp);
  165.  
  166.         if (idist>=SpotList[i].radiusx)
  167.             continue;
  168.  
  169.         intens=IntensTable[idist*100/SpotList[i].radiusx];
  170.  
  171.         akt.r+=intens*SpotList[i].color.r >> 8;
  172.         akt.g+=intens*SpotList[i].color.g >> 8;
  173.         akt.b+=intens*SpotList[i].color.b >> 8;
  174.  
  175.         if (akt.r < 0) akt.r = 0; else if (akt.r > 255) akt.r = 255;
  176.         if (akt.g < 0) akt.g = 0; else if (akt.g > 255) akt.g = 255;
  177.         if (akt.b < 0) akt.b = 0; else if (akt.b > 255) akt.b = 255;
  178.         }
  179.  
  180.         if(imsg=(struct IntuiMessage *)GetMsg(win->UserPort))
  181.         {
  182.         ReplyMsg((struct Message *)imsg);
  183.         quit=1;
  184.         break;
  185.         }
  186.         MC_PutPixel(mch,x,y,&akt);
  187.     }
  188.     }
  189.  
  190.     while(!quit)
  191.     {
  192.     WaitPort(win->UserPort);
  193.     while(imsg=(struct IntuiMessage *)GetMsg(win->UserPort))
  194.     {
  195.         iclass  =imsg->Class;
  196.         icode   =imsg->Code;
  197.         ReplyMsg((struct Message *)imsg);
  198.         switch(iclass)
  199.         {
  200.         case IDCMP_RAWKEY:
  201.         switch(icode)
  202.         {
  203.         case 0x45:        /* ESC */
  204.         case 0x40:        /* Space */
  205.             quit=1;break;
  206.         }
  207.         break;
  208.         }
  209.     }
  210.     }
  211. }
  212.  
  213. void ReadSpots(char *name)
  214. {
  215.     FILE *in;
  216.     char spotline[256];
  217.     int x,y,rx,ry;
  218.     double r,g,b;
  219.  
  220.     spotanz=0;
  221.     if(in=fopen(name,"rb"))
  222.     {
  223.     while(!feof(in))
  224.     {
  225.         fgets(spotline,79,in);
  226.  
  227.         sscanf (spotline,"%i %i %lg %lg %lg %i %i",
  228.         &x,&y,&r,&g,&b,&rx,&ry);
  229.  
  230.         SpotList[spotanz].x=x;
  231.         SpotList[spotanz].y=y;
  232.         SpotList[spotanz].color.r=r*255;
  233.         SpotList[spotanz].color.g=g*255;
  234.         SpotList[spotanz].color.b=b*255;
  235.         SpotList[spotanz].radiusx=rx;
  236.         SpotList[spotanz].radiusy=ry;
  237.  
  238.         spotanz++;
  239.     }
  240.     fclose(in);
  241.     }
  242. }
  243.  
  244. double Intensity(double dist)
  245. {
  246.     return(0.75+(0.25*cos(dist)*(2.0-cos(dist))));
  247. }
  248.  
  249.  
  250. void Usage(void)
  251. {
  252.     printf("Usage \n");
  253.     printf("\tmc_spots typ res name\n");
  254.     printf("\tres\\typ | 0=ECS | 1=AGA,GFX-Card\n");
  255.     printf("\t--------+-------+---------------\n");
  256.     printf("\t e (ehb)| 64    | -             \n");
  257.     printf("\t l (low)| 32    | 256           \n");
  258.     printf("\t h (hi )| 16    | 256           \n");
  259.     printf("\t s (shi)| --    | 256           \n");
  260.     printf("\t--------+-------+---------------\n");
  261.     printf("\n\tname  spotliste\n");
  262. }
  263.  
  264. void main(int argc,char *argv[])
  265. {
  266.     UBYTE dep,typ,fail=0;
  267.     char res;
  268.  
  269.     if(argc==4)
  270.     {
  271.     typ=atoi(argv[1])&1;
  272.     res=argv[2][0];
  273.  
  274.     switch(typ)
  275.     {
  276.     case 0:     /* ECS */
  277.         switch(res)
  278.         {
  279.         case 'E':
  280.         case 'e':
  281.         scrtags[4].ti_Data=dep=6;
  282.         wintags[2].ti_Data=scrtags[2].ti_Data=354;        /* 236 */
  283.         wintags[3].ti_Data=scrtags[3].ti_Data=552;        /* 276 */
  284.         scrtags[7].ti_Data|=EXTRAHALFBRITELACE_KEY;
  285.         break;
  286.         case 'L':
  287.         case 'l':
  288.         scrtags[4].ti_Data=dep=5;
  289.         wintags[2].ti_Data=scrtags[2].ti_Data=354;        /* 236 */
  290.         wintags[3].ti_Data=scrtags[3].ti_Data=552;        /* 276 */
  291.         scrtags[7].ti_Data|=LORESLACE_KEY;
  292.         break;
  293.         case 'H':
  294.         case 'h':
  295.         scrtags[4].ti_Data=dep=4;
  296.         wintags[2].ti_Data=scrtags[2].ti_Data=708;        /* 472 */
  297.         wintags[3].ti_Data=scrtags[3].ti_Data=552;        /* 276 */
  298.         scrtags[7].ti_Data|=HIRESLACE_KEY;
  299.         break;
  300.         case 'S':
  301.         case 's':
  302.         fail=1;
  303.         break;
  304.         }
  305.         break;
  306.     case 1:     /* AGA,GFX-Card */
  307.         switch(res)
  308.         {
  309.         case 'E':
  310.         case 'e':
  311.         fail=1;
  312.         break;
  313.         case 'L':
  314.         case 'l':
  315.         scrtags[4].ti_Data=dep=8;
  316.         wintags[2].ti_Data=scrtags[2].ti_Data=354;        /* 236 */
  317.         wintags[3].ti_Data=scrtags[3].ti_Data=552;        /* 276 */
  318.         scrtags[7].ti_Data|=LORESLACE_KEY;
  319.         break;
  320.         case 'H':
  321.         case 'h':
  322.         scrtags[4].ti_Data=dep=8;
  323.         wintags[2].ti_Data=scrtags[2].ti_Data=708;        /* 472 */
  324.         wintags[3].ti_Data=scrtags[3].ti_Data=552;        /* 276 */
  325.         scrtags[7].ti_Data|=HIRESLACE_KEY;
  326.         break;
  327.         case 'S':
  328.         case 's':
  329.         scrtags[4].ti_Data=dep=8;
  330.         wintags[2].ti_Data=scrtags[2].ti_Data=1416;        /* 944 */
  331.         wintags[3].ti_Data=scrtags[3].ti_Data=552;        /* 276 */
  332.         scrtags[7].ti_Data|=SUPERLACE_KEY;
  333.         break;
  334.         }
  335.         break;
  336.     }
  337.  
  338.     if(!fail)
  339.     {
  340.         OpenAll();
  341.         if(mch=MC_Init(scr,win,dep))
  342.         {
  343.         ReadSpots(argv[3]);
  344.         Spots();
  345.         }
  346.     }
  347.     else Usage();
  348.     }
  349.     else Usage();
  350.     CloseAll();
  351. }
  352.